package com.bootdo.common.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class AliyunOssUtils {

    public void deleteImage(String imageName) {
        System.out.println(imageName);

        String endpoint = AliyunOSSConfigConstant.END_POINT;
        String accessKeyId = AliyunOSSConfigConstant.AccessKey_ID;
        String accessKeySecret = AliyunOSSConfigConstant.AccessKey_Secret;
        String bucketName = AliyunOSSConfigConstant.BUCKE_NAME;
        // EndPoint以上海为例
        // 阿里云API秘钥
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        client.deleteObject(bucketName, imageName);
        client.shutdown();
    }


    /**
     * 上传文件
     */
    public String upLoad(File file) {
        System.out.println("=++++++++++=======+++++++=======+++++++=====+++++++====");

        String endpoint = AliyunOSSConfigConstant.END_POINT;
        String accessKeyId = AliyunOSSConfigConstant.AccessKey_ID;
        String accessKeySecret = AliyunOSSConfigConstant.AccessKey_Secret;
        String bucketName = AliyunOSSConfigConstant.BUCKE_NAME;
        String fileHost = AliyunOSSConfigConstant.FILE_HOST;

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        // 判断文件
        if (file == null) {
            return null;
        }
        String fileUrl = "";
        String dateStr = format.format(new Date());
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            // 判断容器是否存在,不存在就创建
            if (!client.doesBucketExist(bucketName)) {
                client.createBucket(bucketName);
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                client.createBucket(createBucketRequest);
            }
            // 设置文件路径和名称
            fileUrl = fileHost + "/" + (dateStr + "/" + UUID.randomUUID().toString().replace("-", "") + "-" + file.getName());
            // 上传文件
            PutObjectResult result = client.putObject(new PutObjectRequest(bucketName, fileUrl, file));
            // 设置权限(公开读)
            client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            if (result != null) {
                System.out.println("------OSS文件上传成功------" + fileUrl);
            }

        } catch (OSSException oe) {
            System.out.println(oe.getMessage());
        } catch (ClientException ce) {
            System.out.println(ce.getErrorMessage());
        } finally {
            if (client != null) {
                client.shutdown();
            }
        }
        return fileUrl;
    }
}

